package com.allen.action;

import com.allen.bean.User;
import com.allen.dao.UserDao;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.util.List;

public class UserAction extends ActionSupport {
    private Integer id;
    private String username;
    private String password;
    private int age;
    private int sex;
    private String tip;

    public String login(){
        boolean flag = new UserDao().login(username, password);
        if(flag){
            return SUCCESS;
        }else {
            setTip("用户名或密码错误");
            return "login";
        }
    }
    public String list(){
        List<User> users = new UserDao().list();
        ActionContext.getContext().put("users",users);
        return "list";
    }
    public String edit(){
        User user =new UserDao().findById(id);
        ActionContext.getContext().put("user",user);
        return "edit";
    }
    public String update(){
        User user = new User(id,username,password,age,sex);
        new UserDao().update(user);
        return "tolist";
    }
    public String delete(){
        new UserDao().delete(id);
        return "tolist";
    }
    public String toadd(){
        return "toadd";
    }
    public String add(){
        User user = new User(null,username,password,age,sex);
        new UserDao().add(user);
        return "tolist";
    }
    public String find(){
        List<User> users = new UserDao().find(username);
        ActionContext.getContext().put("users",users);
        return "list";
    }
    public String exit(){
        ServletActionContext.getRequest().getSession().invalidate();
        return "login";
    }
    public String regist(){
        User user = new User(null,username,password,age,sex);
        new UserDao().add(user);
        return "login";
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}
